<template>
    <div>
        <NavHeader></NavHeader>
        <ProductParam :scrollHeight="scrollHeight"></ProductParam>
        <div class="detail">
            <div class="swiper">
                <swiper
                    class="swiImgs"
                    :options="swiperOption"
                    v-if="swiperImg.length != 0"
                >
                    <swiper-slide
                        v-for="(item, index) in swiperImg"
                        data-index="index"
                        :key="index"
                    >
                        <img
                            class="swiImg"
                            :src="require('@/assets/imgs/detail/' + item)"
                        />
                    </swiper-slide>
                    <div class="swiper-pagination" slot="pagination"></div>
                </swiper>
            </div>
            <div class="goodsInfo">
                <div class="title">红米Note 7</div>
                <div class="describe">
                    相机全新升级 / 960帧超慢动作 / 手持超级夜景 /
                    全球首款双频GPS / 骁龙845处理器 / 红<br />外人脸解锁 /
                    AI变焦双摄 / 三星 AMOLED 屏
                </div>
                <div class="shop">小米自营</div>
                <div class="price">
                    <span class="newPrice">999元</span>
                    <span class="oldPrice">1999元</span>
                </div>
                <div class="line"></div>
                <div class="address">
                    <img src="@/assets/imgs/detail/icon-loc.png" alt="" />
                    <div>
                        <div>北京 北京市 朝阳区 安定门街道</div>
                        <div class="available">有现货</div>
                    </div>
                </div>
                <div class="select">选择版本</div>
                <div class="version">
                    <div
                        class="version"
                        v-for="(item, index) in attr"
                        :key="index"
                    >
                        <div
                            class="attr"
                            @click="checkType(index)"
                            :class="checkIndex == index ? 'active' : ''"
                        >
                            {{ item }}
                        </div>
                    </div>
                </div>
                <div class="select">选择颜色</div>
                <div class="attr active">深空灰</div>
                <div class="selectVersion">
                    <div class="info">
                        <div>红米Note 7 {{ text }} 深灰色</div>
                        <div>999元</div>
                    </div>
                    <div class="totalPrice">总计：999元</div>
                </div>
                <div class="btn">加入购物车</div>
            </div>
        </div>
        <NavFooter></NavFooter>
    </div>
</template>

<script>
import NavHeader from "../components/NavHeader.vue";
import NavFooter from "../components/NavFooter.vue";
import ProductParam from "@/components/ProductParam";
export default {
    name: "Detail",
    data() {
        return {
            swiperImg: [
                "phone-1.jpg",
                "phone-2.jpg",
                "phone-3.jpg",
                "phone-4.jpg",
            ],
            imgIndex: 1,
            swiperOption: {
                noNextTick: true,
                loop: true,
                //设定初始化时slide的索引
                initialSlide: 0,
                //自动播放
                autoplay: {
                    delay: 1500,
                    stopOnLastSlide: false,
                    disableOnInteraction: false,
                },
                speed: 800,
                direction: "horizontal",
                grabCursor: true,

                on: {
                    slideChangeTransitionStart: function () {
                        this.imgIndex = this.realIndex + 1; //获取轮播图片下标索引；这里有一个坑，之前网上找到的是用activeIndex，但后来网上说的是这个realIndex，原来activeIndex是swiper2.0的；而realIndex是swiper3.0的，（使用realIndex才实现了下标索引）
                    },
                },
                pagination: {
                    el: ".swiper-pagination",
                },
            },
            attr: ["6GB+64GB 全网通", "4GB+64GB 移动4G"],
            text: "6GB+64GB 全网通",
            checkIndex: 0,
            scrollHeight: 0,
        };
    },
    mounted() {
        window.addEventListener("scroll", this.handleScroll, false);
        console.log(this.$refs.product);
    },
    methods: {
        checkType(index) {
            this.text = this.attr[index];
            this.checkIndex = index;
        },
        handleScroll() {
            let clientHeight =
                document.documentElement.clientHeight ||
                document.body.clientHeight;
            let scrollHeight = document.documentElement.scrollTop;
            this.scrollHeight = scrollHeight;
        },
    },
    beforeDestroy() {
        window.removeEventListener("scorll", this.handleScroll);
    },
    components: {
        NavHeader,
        NavFooter,
        ProductParam,
    },
};
</script>

<style lang="less">
.detail {
    margin-top: 50px;
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    padding: 0 50px 0 30px;
    margin-bottom: 30px;
    .goodsInfo {
        .title {
            font-size: 30px;
            font-weight: bold;
            margin-bottom: 10px;
        }
        .describe {
            font-size: 14px;
            color: #666;
            margin-bottom: 20px;
        }
        .shop {
            color: #f60;
            margin-bottom: 10px;
        }
        .price {
            .newPrice {
                color: #f60;
            }
            .oldPrice {
                color: #999;
                text-decoration: line-through;
                font-size: 14px;
                margin-left: 5px;
            }
        }
        .line {
            width: 100%;
            height: 1px;
            background-color: #ccc;
            margin: 15px 0 20px;
        }
        .address {
            border: 1px solid #ccc;
            background-color: #fafafa;
            display: flex;
            height: 100px;
            padding: 30px 0 0 20px;
            box-sizing: border-box;
            img {
                width: 15px;
                height: 18px;
                margin-right: 10px;
            }
            div {
                font-size: 12px;
                color: #666;
            }
            .available {
                color: #f60;
                margin-top: 10px;
            }
        }
        .select {
            font-size: 18px;
            font-weight: bold;
            margin: 20px 0;
        }
        .version {
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .attr {
            width: 270px;
            height: 40px;
            border: 1px solid #ccc;
            text-align: center;
            line-height: 40px;
            font-size: 13px;
            color: #333;
        }
        .active {
            border: 1px solid #f60;
            color: #f60;
        }
        .selectVersion {
            margin-top: 30px;
            background-color: #fafafa;
            width: 100%;
            height: 80px;
            padding: 20px 20px 10px;
            box-sizing: border-box;
            .info {
                display: flex;
                align-items: center;
                justify-content: space-between;
                font-size: 12px;
                color: #333;
                margin-bottom: 10px;
            }
            .totalPrice {
                color: #f60;
                font-size: 18px;
            }
        }
        .btn {
            width: 250px;
            height: 40px;
            background-color: #f60;
            color: #fff;
            font-size: 16px;
            text-align: center;
            line-height: 40px;
            margin-top: 20px;
        }
    }
}
.swiper {
    width: 800px;
    .swiImg {
        width: 100%;
    }
}
</style>